// $CertusJsonAjax.ajax("queryCCBXLCProcessTrace",{
//     "processInstanceId": $CertusJsonAjax.getProInstanceId(),
//     "stepInstanceId": $CertusJsonAjax.getStepInstanceId()
// },function(res){
//     ;
//     $$scope.traceRecord = res.data;
// })

$CertusJsonAjax.ajax("printInfoCCBX", {
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId()
}, function (res) {;
    $$scope.printDataList = res.data;
    var compName = $$scope.printDataList[0].totalEntity.compName;
    var list = $$scope.printDataList[0].pageItemList;
    var aplength = $$scope.printDataList[0].singProcess.length;

    //console.log(compName);
    tagName(compName);

    //console.log(list);
    listSum(list);

    //console.log(aplength);
    shapplog(aplength);
    
});

function shapplog(aplength){
    if(aplength == 0){
        $$scope.applog = false;
        $$scope.apploged = true;
        return $$scope.applog;
    }else{
        $$scope.applog = true;
        $$scope.apploged = false;
        return $$scope.applog;
    }
};


function tagName(compName){
    $$scope.compName = "";
    if(compName == "STSJT"){
        $$scope.compName = "南京";
    }
    if(compName == "STSNJ"){
        $$scope.compName = "南京";
    }
    if(compName == "STSBJ"){
        $$scope.compName = "北京";
    }
    if(compName == "STSGD"){
        $$scope.compName = "广东";
    }
    if(compName == "STSSH"){
        $$scope.compName = "上海";
    }
    return $$scope.compName;
};


function listSum(list) {

    $$scope.list1 = 0;
    $$scope.list2 = 0;
    $$scope.list3 = 0;
    $$scope.list4 = 0;
    $$scope.list5 = 0;
    $$scope.list6 = 0;
    $$scope.list7 = 0;
    $$scope.list8 = 0;

    for (var i = 0; i < list.length; i++) {
        $$scope.list1 += parseInt(list[i].indoorTraffic);
        $$scope.list2 += parseInt(list[i].officeExpenses);
        $$scope.list3 += parseInt(list[i].entertainDinner);
        $$scope.list4 += parseInt(list[i].consumable);
        $$scope.list5 += parseInt(list[i].lunchAmount);
        $$scope.list6 += parseInt(list[i].communicationFee);
        $$scope.list7 += parseInt(list[i].carFee);
        $$scope.list8 += parseInt(list[i].elseFee);
    }

    return $$scope.list1, $$scope.list2, $$scope.list3, $$scope.list4, $$scope.list5, $$scope.list6, $$scope.list7, $$scope.list8;

};


$$scope.getExpenseType = function (a) {
    if (a == 'trmType') {
        return "出差报销";
    } else if (a == "cashType") {
        return "现金报销";
    }
};

$CertusJsonAjax.ajax("printInfoCcbxBase", {
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId()
}, function (res) {
    $$scope.ccbxItemRepeatss = res.data;
    $$scope.rmb = upDigit($$scope.ccbxItemRepeatss.totalAmount);
});

// RMB大写转换
function upDigit(n) {
    var fraction = ['角', '分'];
    var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
    var unit = [
        ['元', '万', '亿'],
        ['', '拾', '佰', '仟']
    ];
    var head = n < 0 ? '欠' : '';
    n = Math.abs(n);

    var s = '';

    for (var i = 0; i < fraction.length; i++) {
        s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
    }
    s = s || '整';
    n = Math.floor(n);

    for (var i = 0; i < unit[0].length && n > 0; i++) {
        var p = '';
        for (var j = 0; j < unit[1].length && n > 0; j++) {
            p = digit[n % 10] + unit[1][j] + p;
            n = Math.floor(n / 10);
        }
        s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
    }
    return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
}


$$scope.printpageBX = function () {
    var cc = "1";
    event.preventDefault();

    html2canvas(document.getElementById("containerPrint-" + cc), {
        allowTaint: true,
        taintTest: false,
        onrendered: function (canvas) {
            canvas.id = "mycanvas";
            //生成base64图片数据
            var dataUrl = canvas.toDataURL();
            var newImg = document.createElement("img");
            newImg.id = "printImg-" + cc;
            newImg.src = dataUrl;
            $('.printImg-' + cc).show();
            document.getElementById('printImgBox-' + cc).appendChild(newImg);
            $('#printImg-' + cc).jqprint({
                debug: false,
                importCSS: true,
                printContainer: true,
                operaSupport: false
            });
        }
    })
}

$$scope.closePrintViewCCBX = function () {
    layer.confirm('已递交打印的纸质单据？', {
        btn: ['已递交', '未递交'] //按钮
    }, function () {
        $CertusJsonAjax.ajax("ccbxPrintApproval", {
            "processInstanceId": $CertusJsonAjax.getProInstanceId(),
            "stepInstanceId": $CertusJsonAjax.getStepInstanceId(),
            // "financialUserId": c
        }, function (res) {
            if (res.data) {
                if (res.data == "success") {
                    window.history.back();
                }
                layer.msg('转入财务审核', {icon: 1});
                window.history.back();
            }
        })
    }) 
};